﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using VDataAdapter.EventManagement;

namespace VDataAdapter.QueueManagement
{
    enum EventType
    {
        FirstTime,
        Stock,
        Transaction,
        PutThrough,
        PutThroughTransaction,
        MarketInfo
    };

    class EventQueueChecker
    {
        protected IEventProviderQueue _queue;

        public EventQueueChecker(IEventProviderQueue queue)
        {
            _queue = queue;
        }

        public EventQueueChecker(IEventProviderQueue queue, int timeOut)
        {
            _queue = queue;
           
             
        }

        protected bool _stopCheckingQueue = false;
        public bool StopCheckingQueue
        {
            get { return _stopCheckingQueue; }
            set { _stopCheckingQueue = value; }
        }


        /// <summary>
        /// Checking if there is event in Queue
        /// Will block until there is an Event return
        /// Can stop by setting StopCheckingQueue
        /// </summary>
        /// <returns></returns>
        public virtual Event CheckingQueue(EventType type)
        {

            Event e = null;

            while (!_stopCheckingQueue)
            {
                
                switch (type)
                {
                    case EventType.FirstTime: e = _queue.GetFirstTimeEvent();
                        break;
                    case EventType.Stock: e = _queue.GetTop3PricesEvent();
                        break;
                    case EventType.Transaction: e = _queue.GetTransactionsEvent();
                        break;
                    case EventType.PutThrough: e = _queue.GetPtTopPricesEvent();
                        break;
                    case EventType.PutThroughTransaction: e = _queue.GetPtTransactionsEvent();
                        break;
                    case EventType.MarketInfo: e = _queue.GetMarketInfosEvent();
                        break;
                }

                if (e == null)
                {
                    Thread.Sleep(50);
                }
                else
                {
                    return e;
                }
            }
            return null;
        }
    }
}
